import 'package:flutter_menu/app_config.dart';
import 'package:flutter_menu/biz/order/detail/option/options_dialog.dart';
import 'package:flutter_menu/biz/order/detail/price/price_dialog.dart';
import 'package:flutter_menu/biz/order/order_pg.dart';
import 'package:flutter_menu/biz/order/widget/bottom_tab_bar.dart';
import 'package:flutter_menu/biz/order/widget/count_number_button.dart';
import 'package:flutter_menu/gen/assets.gen.dart';
import 'package:flutter_menu/res/app_design.dart';
import 'package:flutter_menu/utils/common_utils.dart';
import 'package:get/get.dart';

import 'detail_controller.dart';

final buttonSize = 94.w;
const countNumberButtonActive = AppColors.countNumberButtonActive;

class DetailPg extends GetView<DetailController> {
  const DetailPg({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DetailController>(
        init: DetailController(),
        builder: (controller) => body(context).whitScaffold());
  }

  Widget body(BuildContext context) {
    Shadow textShadow = Shadow(
      offset: const Offset(0, 2.0),
      blurRadius: 4.0,
      color: AppColors.text_9d.withOpacity(0.5),
    );
    var country = AppConfig.instance.country ?? Country.indonesia;
    var hotZone = controller.parseHotZoneFromJson();
    final left = hotZone.left.sw;
    final right = (1 - hotZone.right).sw;
    final top = hotZone.top.sh;
    final bottom = (1 - hotZone.bottom).sh;
    return Stack(
      children: [
        cachedNetworkImage(imageUrl: controller.productImageUrl),
        controller.productIsRead
            ? Stack(
                children: [
                  Positioned(
                    right: 32.w,
                    top: 120.h,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Text(controller.productName,
                            style: TextStyle(
                              height: 1.4,
                              fontWeight: AppStyle.semiBold,
                              color: AppColors.white,
                              fontSize: 55.sp,
                              shadows: [
                                textShadow,
                              ],
                            )),
                        SizedBox(
                          height: 4.w,
                        ),
                        Obx(() {
                          return Text(
                              '${country.unit} ${formatPrice(controller.productPrice, decimalDigits: country.decimalDigits) ?? ''}',
                              style: TextStyle(
                                  height: 1.4,
                                  fontWeight: AppStyle.semiBold,
                                  color: AppColors.white,
                                  fontSize: 55.sp,
                                  shadows: [
                                    textShadow,
                                  ]));
                        })
                      ],
                    ),
                  ),
                  Obx(() {
                    return Positioned(
                      right: 32.w,
                      bottom: 48.h + mainTabHeight,
                      child: controller.selectedProductNumber.value == 0
                          ? (controller.isMultiSpec()
                              ? buildMultiSpecAddWidget()
                              : buildMealAddWidget(
                                  buttonSize, countNumberButtonActive))
                          : buildDishNumberEditWidget(
                              buttonSize, countNumberButtonActive),
                    );
                  })
                ],
              )
            : Positioned(
                left: left,
                right: right,
                top: top,
                bottom: bottom,
                child: GestureDetector(
                  onTap: controller.showMealOptionsSelectDialog,
                  child: Container(
                    color: Colors.transparent, // 热区是透明的
                  ),
                ),
              ),
      ],
    );
  }

  //多规格添加按钮
  Widget buildMultiSpecAddWidget() {
    var buttonHeight = 90;
    return GestureDetector(
      onTap: () {
        controller.showMealOptionsSelectDialog();
      },
      child: Container(
          alignment: Alignment.center,
          height: buttonHeight.w,
          padding: EdgeInsets.symmetric(horizontal: 60.w),
          decoration: BoxDecoration(
            color: countNumberButtonActive,
            borderRadius: BorderRadius.circular((buttonHeight / 2).r),
          ),
          child: Text("orderSelectSpec".tr,
              style: TextStyle(
                  fontWeight: AppStyle.semiBold,
                  color: AppColors.white,
                  fontSize: 32.sp))),
    );
  }

  //非多规格添加按钮
  Widget buildMealAddWidget(double buttonSize, Color countNumberButtonActive) {
    return GestureDetector(
      onTap: () {
        controller.showMealOptionsSelectDialog();
      },
      child: SizedBox(
        width: buttonSize,
        height: buttonSize,
        child: Container(
          padding: EdgeInsets.all(8.w),
          decoration: BoxDecoration(
              color: countNumberButtonActive, shape: BoxShape.circle),
          child: Stack(
            alignment: Alignment.center,
            // Center alignment for the cross
            children: [
              // 横线
              Container(
                width: buttonSize * 0.65, // 横线的宽度
                height: CountNumberButton.iconThickness, // 横线的高度
                color: Colors.white, // 横线的颜色
              ),
              // 竖线
              Container(
                width: CountNumberButton.iconThickness, // 竖线的宽度
                height: buttonSize * 0.65, // 竖线的高度
                color: Colors.white, // 竖线的颜色
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget buildDishNumberEditWidget(
      double buttonSize, Color countNumberButtonActive) {
    return CountNumberButton(
      initialValue: controller.selectedProductNumber.value,
      minValue: 0,
      maxValue: 9999,
      width: 315.w,
      height: buttonSize,
      maxLength: 4,
      colorActive: countNumberButtonActive,
      colorInactive: AppColors.countNumberButtonInactive,
      colorBg: AppColors.countNumberBg,
      textStyle: TextStyle(
          fontSize: 44.sp,
          color: AppColors.textInBottomSheet,
          fontWeight: AppStyle.semiBold),
      incrementInterceptor: () {
        return controller.isMultiSpec() || controller.isPackage();
      },
      onChanged: (value, isAdd) {
        // Handle counter value changes
        debugPrint('Selected value: $value $isAdd');
        if ((controller.isMultiSpec() || controller.isPackage()) && isAdd) {
          controller.showMealOptionsSelectDialog();
        } else {
          controller.updateProductNumber(value.toInt());
        }

      },
    );
  }


  // 不展示时价输入弹窗，改为Pos端输入
  void showRealTimePricingDialog() {
    Get.dialog(
      const PriceInputDialog(),
      transitionCurve: Curves.easeIn,
      transitionDuration: const Duration(milliseconds: 100),
    );
  }
}
